﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using Com;
using Controllers;
using NLog;

namespace mvc4
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            AreaRegistration.RegisterAllAreas();
            GlobalFilters.Filters.Add(new HandleErrorAttribute());
            //webapi注册模块
//            GlobalConfiguration.Configure(configuration => {
//
//                var cors = new EnableCorsAttribute("www.example.com", "*", "*");
//                configuration.EnableCors(cors);
//                configuration.Routes.MapHttpRoute(
//                    name: "DefaultApi",
//                    routeTemplate: "api/{controller}/{id}",
//                    defaults: new { id = RouteParameter.Optional }
//                );
//            });

            var routes = RouteTable.Routes;
            routes.MapMvcAttributeRoutes();
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

        private void Bootstrapper_BeginRequest(object sender, EventArgs e)
        {
            if(HttpContext.Current==null)
                return;
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","http://localhost:8080");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age","1728000");
            if (HttpContext.Current.Request.HttpMethod.Equals("OPTIONS", StringComparison.OrdinalIgnoreCase))
            {
                HttpContext.Current.Response.End();
            }
        }
        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            Bootstrapper_BeginRequest(sender, e);
        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {
            HttpException erroy = Server.GetLastError() as HttpException;
            if (erroy != null && erroy.GetHttpCode() == 404)
            {
                Server.ClearError();
                Response.StatusCode = 404;
                Response.End();
                return; 
            }
            var ex= Server.GetLastError().GetBaseException();
            var logger = LogManager.GetCurrentClassLogger();
            logger.Error(ex);
        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}